<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class CreateTenantPositionTable extends AbstractMigration
{
    public function up()
    {
        $table = $this->table('tenant_position', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '职位表']);
        //添加数据字段
        $table->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '职位ID'])
            ->addColumn('name', 'string', ['null' => false, 'default' => '', 'limit' => 60, 'comment' => '职位名称'])
            ->addColumn('tenant_id', 'integer', ['signed' => false, 'default' => 0, 'limit' => 11, 'comment' => '租户ID'])
            ->addColumn('created', 'integer', ['signed' => false, 'default' => 0, 'limit' => 11, 'comment' => '创建时间'])
            ->addColumn('created_by', 'integer', ['signed' => false, 'default' => 0, 'limit' => 11, 'comment' => '创建者'])
            ->addColumn('updated', 'integer', ['signed' => false, 'default' => 0, 'limit' => 11, 'comment' => '更新时间'])
            ->addIndex(['tenant_id', 'name'], ['type' => 'unique', 'name' => 'idx_unique_tenant_name']);

        //执行创建
        $table->create();

        // 租户用户添加职位表
        $this->table('tenant_user')->addColumn('tenant_position_id', 'integer', [
            'limit' => 11,
            'null' => false,
            'signed' => false,
            'default' => 0,
            'comment' => '职位id'
        ])->save();
    }

    public function down()
    {
        $this->table('tenant_position')->drop()->save();
        $this->table('tenant_user')->removeColumn('tenant_position_id')->save();
    }
}
